import { defineStore } from 'pinia'
import type { LeaderCounselorRelation } from '../types/LeaderCounselorRelation'

/**
 * 领导辅导员关系存储模块
 * 负责管理领导与辅导员关系的全局状态
 */
export const useLeaderCounselorRelationStore = defineStore('leaderCounselorRelation', {
    // 持久化配置
    persist: {
        storage: localStorage,
        paths: ['relations', 'currentRelation']
    },
    state: () => ({
        // 所有关系列表
        relations: [] as LeaderCounselorRelation[],
        // 当前选中的关系
        currentRelation: null as LeaderCounselorRelation | null,
        // 加载状态
        loading: false
    }),
    actions: {
        /**
         * 获取所有领导辅导员关系
         */
        async fetchAllRelations() {
            this.loading = true
            try {
                // this.relations = await relationApi.getAllLeaderCounselorRelations()
            } finally {
                this.loading = false
            }
        },

        /**
         * 建立新的领导辅导员关系
         * @param leaderId - 领导ID
         * @param counselorId - 辅导员ID
         */
        async createRelation(leaderId: number, counselorId: number) {
            this.loading = true
            try {
                // const newRelation = await relationApi.createLeaderCounselorRelation(leaderId, counselorId)
                // this.relations.push(newRelation)
            } finally {
                this.loading = false
            }
        },

        /**
         * 删除领导辅导员关系
         * @param relationId - 关系ID
         */
        async deleteRelation(relationId: number) {
            this.loading = true
            try {
                // await relationApi.deleteLeaderCounselorRelation(relationId)
                // this.relations = this.relations.filter(r => r.relationId !== relationId)
            } finally {
                this.loading = false
            }
        }
    },
    getters: {
        /**
         * 根据领导ID获取关系列表
         * @param leaderId - 领导ID
         */
        relationsByLeaderId: (state) => (leaderId: number) =>
            state.relations.filter(r => r.leaderId === leaderId),

        /**
         * 根据辅导员ID获取关系列表
         * @param counselorId - 辅导员ID
         */
        relationsByCounselorId: (state) => (counselorId: number) =>
            state.relations.filter(r => r.counselorId === counselorId)
    }
})
